setwd("~/Desktop/Compare ASVs_18S & ITS/Filtered 18S +ITS subset")
rm(list=ls())
library(VennDiagram)
library(dplyr)
# List of file paths
files <- list(
"Inoculated_With_P" = "18S_filtered_Inoculated_withP.csv",
"Inoculated_With_MS" = "ITS_Group Inoculated With MS.csv",
"Uninoculated_With_P" = "18S_filtered_Uninoculated_withP.csv",
"Uninoculated_With_MS" = "ITS-Group Uninoculated With MS.csv",
"Inoculated_Without_P" = "18S_filtered_Inoculated_withoutP.csv",
"Inoculated_Without_MS" = "ITS_Group Inoculated Without MS.csv",
"Uninoculated_Without_P" = "18S_filtered_Uninoculated_withoutP.csv",
"Uninoculated_Without_MS" = "ITS-Group Uninoculated Without MS.csv"
)
asv_groups <- lapply(files, function(file) {
data <- read.csv(file, header = TRUE, stringsAsFactors = FALSE)
# Remove rows where Family is NA or empty
data <- data[!is.na(data$Family) & data$Family != "", ]
unique(data$Family)  # Extract unique families after cleaning
})
# Define comparisons
comparisons <- list(
c("Inoculated_With_P", "Inoculated_With_MS"),
c("Uninoculated_With_P", "Uninoculated_With_MS"),
c("Inoculated_Without_P", "Inoculated_Without_MS"),
c("Uninoculated_Without_P", "Uninoculated_Without_MS")
)
# Loop through comparisons
for (comparison in comparisons) {
group1 <- comparison[1]
group2 <- comparison[2]
phyla_g1 <- asv_groups[[group1]]
phyla_g2 <- asv_groups[[group2]]
# Shared and unique phyla
shared_phyla <- intersect(phyla_g1, phyla_g2)
unique_to_g1 <- setdiff(phyla_g1, phyla_g2)
unique_to_g2 <- setdiff(phyla_g2, phyla_g1)
# Display shared and unique phyla in the console
cat("\nComparison:", group1, "vs", group2, "\n")
cat("Shared Phyla:\n", paste(shared_phyla, collapse = ", "), "\n")
cat("Unique to", group1, ":\n", paste(unique_to_g1, collapse = ", "), "\n")
cat("Unique to", group2, ":\n", paste(unique_to_g2, collapse = ", "), "\n")
# Generate labels for Venn diagram
shared_label <- paste("Shared:\n", paste(shared_phyla, collapse = ", "))
unique_to_g1_label <- paste("Unique to", group1, ":\n", paste(unique_to_g1, collapse = ", "))
unique_to_g2_label <- paste("Unique to", group2, ":\n", paste(unique_to_g2, collapse = ", "))
# Generate the Venn diagram
venn.plot <- draw.pairwise.venn(
area1 = length(phyla_g1),                # Total in group1
area2 = length(phyla_g2),                # Total in group2
cross.area = length(shared_phyla),      # Shared count
category = c(group1, group2),           # Labels for the sets
fill = c("blue", "red"),                # Colors for the sets
alpha = 0.5,                            # Transparency
cat.pos = c(0, 180),                    # Label positions
cat.dist = 0.05                         # Distance of labels from circles
)
# Save the Venn diagram
pdf(paste0(group1, "_vs_", group2, "_venn_Family.pdf"))
grid.draw(venn.plot)
# # Overlay phylum names
# grid.text(shared_label, x = 0.5, y = 0.6, gp = gpar(fontsize = 8, col = "black"))
# grid.text(unique_to_g1_label, x = 0.3, y = 0.8, gp = gpar(fontsize = 8, col = "blue"))
# grid.text(unique_to_g2_label, x = 0.7, y = 0.8, gp = gpar(fontsize = 8, col = "red"))
#
dev.off()
}
